package com.groupfour.attendancemanagementsystem.controller;

import com.groupfour.attendancemanagementsystem.entity.DTO.TeacherAddDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.TeacherFindDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.TeacherUpdateDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.EmploymentStatusAndTeacherCode;
import com.groupfour.attendancemanagementsystem.service.TeacherService;
import com.groupfour.attendancemanagementsystem.utils.MD5;
import com.groupfour.attendancemanagementsystem.utils.Result;
import com.groupfour.attendancemanagementsystem.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @version 1.0
 * @Author 199000535李泓静
 * @Date 2022/4/20 14:41
 * @注释
 */
@RestController
@RequestMapping("/teacherManagement")
public class TeacherInformationController {

    @Autowired
    private TeacherService teacherService;


    /**
     * @author li
     * @param pageNum,teacherFindDTO
     * @return
     * @describe 按条件查询教师信息
     */
    @PostMapping("/selectTeacherByTeacherFindDTO/{pageNum}/{pageSize}")
    public Result findTeacherByTeacherFindDTO(
            @RequestBody TeacherFindDTO teacherFindDTO,
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize){
        Map<String,Object> teacherMap = teacherService.findTeacherByTeacherFindDTO(teacherFindDTO,pageNum,pageSize);
        Long num = (Long) teacherMap.get("pageCount");
        if (num > 0) {
            return Result.ok(teacherMap);
        }
        return Result.fail(ResultCodeEnum.SELECT_ERROR.getMessage());
    }

    /**
     * @author li
     * @param teacherAddDTO
     * @return
     * @describe 添加教师信息
     */
    @PostMapping("/addTeacher")
    public Result AddTeacher(@RequestBody TeacherAddDTO teacherAddDTO) {
        teacherAddDTO.setTeacherPassword(teacherAddDTO.getTeacherIdentity().substring(teacherAddDTO.getTeacherIdentity().length() - 6));
        teacherAddDTO.setTeacherPassword(MD5.encrypt(teacherAddDTO.getTeacherPassword()));
        int addTeacher = teacherService.AddTeacher(teacherAddDTO);
        switch (addTeacher) {
            case 1:
                return Result.fail("添加失败");
            case 2:
                return Result.fail("添加失败，工号重复！");
            default:
                return  Result.ok("添加成功");

        }

    }

    /**
     * @author li
     * @param teacherUpdateDTO
     * @return
     * @describe 修改教师基本信息（工号不允许修改）
     */
    @PostMapping("/updateTeacher")
    public Result UpdateTeacher(@RequestBody TeacherUpdateDTO teacherUpdateDTO){
        Date date = new Date(System.currentTimeMillis());
        System.out.println(date);
        teacherUpdateDTO.setUpdateTime(date);
        int result = teacherService.UpdateTeacher(teacherUpdateDTO);
        if(result > 0){
            return Result.ok().message("修改教师基本信息成功");
        }
        return Result.fail().message("修改教师基本信息失败");
    }

    /**
     * @author li
     * @param employmentStatusAndTeacherCode
     * @return
     * @describe 修改老师任职状态
     */
    @PostMapping("/updateTeacherEmploymentStatus")
    public Result UpdateTeacherEmploymentStatus(@RequestBody EmploymentStatusAndTeacherCode employmentStatusAndTeacherCode){
        int result = teacherService.UpdateTeacherEmploymentStatus(employmentStatusAndTeacherCode);
        if(result > 0){
            return Result.ok().message("修改任职状态成功");
        }
        return Result.fail().message("修改任职状态失败");
    }
}

